package com.example.androidsx;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class CheckoutActivity extends AppCompatActivity implements CartAdapter.OnItemDeleteListener {

    private CartAdapter cartAdapter;
    private List<CartItem> cartItems = new ArrayList<>();
    private DecimalFormat decimalFormat = new DecimalFormat("¥0.00");

    private RecyclerView rvCartItems;
    private TextView tvSubtotal;
    private TextView tvDiscount;
    private TextView tvDeliveryFee;
    private TextView tvReceiverName;
    private TextView tvReceiverPhone;
    private TextView tvReceiverAddress;
    private TextView tvTotalTextView;
    private RadioGroup rgPayment;
    private Button btnSubmitOrder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkout);

        // 初始化视图
        rvCartItems = findViewById(R.id.rvCartItems);
        tvSubtotal = findViewById(R.id.tvSubtotal);
        tvDiscount = findViewById(R.id.tvDiscount);
        tvDeliveryFee = findViewById(R.id.tvDeliveryFee);
        tvReceiverName = findViewById(R.id.tvReceiverName);
        tvReceiverPhone = findViewById(R.id.tvReceiverPhone);
        tvReceiverAddress = findViewById(R.id.tvReceiverAddress);
        rgPayment = findViewById(R.id.rgPayment);
        btnSubmitOrder = findViewById(R.id.btnSubmitOrder);
        LinearLayout totalLayout = findViewById(R.id.tvTotal);
        tvTotalTextView = totalLayout.findViewById(R.id.tvTotalText);


        // 模拟数据
        setupMockData();

        // 初始化RecyclerView
        cartAdapter = new CartAdapter(cartItems, (CartAdapter.OnItemDeleteListener) this);
        rvCartItems.setLayoutManager(new LinearLayoutManager(this));
        rvCartItems.setAdapter(cartAdapter);

        calculateTotal();

        btnSubmitOrder.setOnClickListener(v -> {
            Intent intent=new Intent(this,PaymentSuccessActivity.class);
            startActivity(intent);
        });

    }

    private void setupMockData() {
        cartItems.add(new CartItem("可口可乐", 3.50, 2, "https://example.com/coke.jpg"));
        cartItems.add(new CartItem("乐事薯片", 6.00, 1, "https://example.com/chips.jpg"));
        cartItems.add(new CartItem("奥利奥饼干", 8.50, 3, "https://example.com/oreo.jpg"));

        tvReceiverName.setText("收货人: " + getString(R.string.sample_receiver_name));
        tvReceiverPhone.setText("电话: " + getString(R.string.sample_receiver_phone));
        tvReceiverAddress.setText("地址: " + getString(R.string.sample_receiver_address));
    }

    private void calculateTotal() {
        double subtotal = 0;
        for (CartItem item : cartItems) {
            subtotal += item.getPrice() * item.getQuantity();
        }

        double discount = subtotal > 50 ? 5.0 : 0.0;
        double deliveryFee = subtotal > 100 ? 0.0 : 5.0;
        double total = subtotal - discount + deliveryFee;

        tvSubtotal.setText("商品总价: " + decimalFormat.format(subtotal));
        tvDiscount.setText("优惠: -" + decimalFormat.format(discount));
        tvDeliveryFee.setText("配送费: " + decimalFormat.format(deliveryFee));
        tvTotalTextView.setText("实付款: " + decimalFormat.format(total));
    }


    private void submitOrder() {
        String paymentMethod;
        int checkedId = rgPayment.getCheckedRadioButtonId();

        if (checkedId == R.id.rbAlipay) {
            paymentMethod = "支付宝";
        } else if (checkedId == R.id.rbWechat) {
            paymentMethod = "微信";
        } else {
            paymentMethod = "未知";
        }

        String amount = tvTotalTextView.getText().toString().replace("实付款: ", "");

        // 跳转到支付成功页面
        Intent intent = new Intent(CheckoutActivity.this, PaymentSuccessActivity.class);
        intent.putExtra("payment_method", paymentMethod);
        intent.putExtra("amount", amount);
        startActivity(intent);

        // 结束当前页面（可选）
        finish();
    }

    @Override
    public void onItemDelete(int position) {
        cartItems.remove(position);
        cartAdapter.notifyItemRemoved(position);

        calculateTotal();

        Toast.makeText(this, "商品已移除", Toast.LENGTH_SHORT).show();
    }
}